package development.perpective;

import java.sql.SQLException;
import java.util.ArrayList;

import run.Compiler;
import DTO.Aspect;
import DTO.ModelGenerated;
import aspect.AspectRepository;
import business.AspectComponents;
import business.AspectFile;

public class AspectRegister {
	private AspectRepository aspectRepository;

	public AspectRegister(AspectRepository AspectRepository){
		this.aspectRepository = AspectRepository;
	}

	public String[] getPropertiesNames() throws SQLException{
		return aspectRepository.getPropertiesNames();
	}
	
	public void joinCode(ModelGenerated modelToExport) throws Exception{
		// ver execoes!!! TODO
		//pegar todas os aspectos TODO 
		AspectComponents componentsToJoinInModel = aspectRepository.getAspectNames(modelToExport.getAspectsSelected());
		
		Compiler.joinCode(componentsToJoinInModel, modelToExport.getPathToImport(), modelToExport.getPathToExport()+"\\"+modelToExport.getName() + ".nlogo");
	}

	public AspectFile addAspect(Aspect aspect, String[] propertiesNames, ExportArchive export) throws SQLException{
		// get all aspect name saved from bd.
		String [] aspectNamesArray = aspectRepository.getAspectNames();

		// validate all components aspect filled by user.
		ValidateScreenData.validateElementsOfAspect(aspect, aspectNamesArray, propertiesNames);

		// join all components and export aspect.
		String archivePath = export.exportAspectArchive( export.joinAspectComponents(aspect) );

		// compiles the code by the compiler.
		String [] paths = {archivePath};
		
		//TODO criar excecao
		AspectFile aspectFile = Compiler.compilesCode(paths);

		if(aspectFile!=null){
			// save aspect on bd. TODO!!
			aspectRepository.addAspect(aspect, aspectFile);
		}
		
		return aspectFile;
	}
	
	public ArrayList<Aspect> getPropertiesAndAspects() throws SQLException{
		return aspectRepository.getPropertiesAndAspects();
	}

}
